<div class="error_message"></div>

<form 
    class  = "form_input"
    action = "<?=Yii::app()->createUrl('account/save')?>"
    method = "post"
    id     = "form_item"
    name   = "form_item"
    target = "integration_asynchronous"
>
    <fieldset class="border_radius_all">
        <legend></legend>

        <div class="form_input_row">
            <label class="label_title">Họ và Tên:*</label>

            <div class="input_item">
                <input
                    id       = "txt_name"
                    name     = "txt_name"
                    title    = "Họ và tên của tên bạn"
                    type     = "text"
                    value    = "<?php if(isset($o_model)) { echo $o_model->name;}?>"
                    class    = "form_textbox textbox_css3"
                    tabindex = "1"
                    onblur   = "validation_empty(this,'lbl_name_validation')"
                />
                <label
                    id    = "lbl_name_validation" 
                    class = "lable_validation"
                >Bạn chưa nhập họ và tên.</label>
            </div>
        </div><!--End form_input_row Name-->

        <div class="form_input_row">
            <label class="label_title">Tên Tài Khoản:*</label>

            <div class="input_item">
                <input
                    id       = "txt_account"
                    name     = "txt_account"
                    title    = "Tên Tài khoản Của Bạn"
                    type     = "text"
                    value    = "<?php if(isset($o_account)) { echo $o_account->identity;}?>"
                    class    = "form_textbox textbox_css3"
                    onblur   = "validation_empty(this,'lbl_account_validation')"
                    tabindex = "2"
                    <?php if(isset($o_account)) { echo 'disabled="disabled"';}?>
                />
                <label
                    id    = "lbl_account_validation" 
                    class = "lable_validation"
                >Bạn chưa nhập tên tài khoản.</label>
            </div>
        </div><!--End form_input_row Account-->

        <div class="form_input_row">
            <label class="label_title">Mật Khẩu:*</label>

            <div class="input_item">
            <input
                id       = "txt_password"
                name     = "txt_password"
                title    = "Mật Khẩu Của Bạn"
                type     = "<?php if(isset($o_account)) { echo "password"; } else "text";?>"
                value    = "<?php if(isset($o_account)) { echo $o_account->password; }?>"
                class    = "form_textbox textbox_css3"
                tabindex = "3"
                disabled = "disabled"
            />
            </div>
        </div><!--End form_input_row Password-->

                <?php                
                if(isset($o_account))
                {
                    $temp_day    = date("d",strtotime($o_account->birth));
                    $temp_month    = date("m",strtotime($o_account->birth));
                    $temp_year    = date("Y",strtotime($o_account->birth));
                }
                ?>
                <div class="form_input_row">
                    <label class="label_title">Ngày Sinh:</label>
                    <div class="input_items_horizon" style="padding-top: 0px;">
                    
                        <select
                            id            = "cb_birth_day"
                            name        = "cb_birth_day"
                            tabindex    = "4"
                            <?php
                            if ($isIEBrowser != true)
                                echo "class = 'select_css3 select_two_arrown'";
                            else
                                echo "class = 'combo_box'";
                            ?>
                        >
                            <?php for( $i = 1; $i <= 31; $i ++ ) {?>
                                <option
                                    value="<?=sprintf( "%02d", $i );?>"
                                    <?php if ( isset($temp_day) && $temp_day == $i) {?>
                                    selected    = "selected"
                                    <?php }?>
                                ><?=sprintf( "%02d", $i );?></option>
                            <?php } ?>
                        </select>
                        
                        <select
                            id        = "cb_birth_month"
                            name    = "cb_birth_month"
                            tabindex= "5"
                            <?php
                            if ($isIEBrowser != true)
                                echo "class = 'select_css3 select_two_arrown'";
                            else
                                echo "class = 'combo_box'";
                            ?>
                        >
                            <?php for( $i = 1; $i <= 12; $i ++ ) { ?>
                                <option
                                    value="<?=sprintf( "%02d", $i );?>"
                                    <?php if ( isset($temp_month) && $temp_month == $i) {?>
                                    selected    = "selected"
                                    <?php }?>
                                ><?=sprintf( "%02d", $i );?></option>
                            <?php } ?>
                        </select>
                        
                        <select
                            id        = "cb_birth_year"
                            name    = "cb_birth_year"
                            tabindex= "6"
                            <?php
                            if ($isIEBrowser != true)
                                echo "class = 'select_css3 select_two_arrown'";
                            else
                                echo "class = 'combo_box'";
                            ?>
                        >
                            <?php for( $i = intval( date('Y') ) - 10; $i > intval( date( 'Y' ) ) - 70; $i -- ) { ?>
                                <option
                                    value="<?=$i;?>"
                                    <?php if ( isset($temp_year) && $temp_year == $i) {?>
                                    selected    = "selected"
                                    <?php }?>
                                ><?=$i;?></option>
                            <?php } ?>
                        </select>
                    </div><!--End input_items_horizon-->
                </div><!--End form_input_row Date-->
                
                <div class="form_input_row">
                    <label class="label_title">Giới Tính:</label>
                    <div class="input_items_horizon">
                    
                        <input
                            title        = "Nam"
                            name        = "ra_sex"
                            id            = "ra_male"
                            type        = "radio"
                            value        = "0"
                            class        = "item_option"
                            tabindex    = "7"
                            <?php if( isset($o_account) && $o_account->is_female == 0 )
                                echo "checked = \"checked\"";
                            else 
                                echo "checked = \"checked\"";
                            ?>
                        /><label>Nam</label>
                        
                        <input
                            title        = "Nữ"
                            name        = "ra_sex"
                            id            = "ra_female"
                            type        = "radio"
                            value        = "1"
                            class        = "item_option"
                            tabindex    = "8"
                            <?php if( isset($o_account) && $o_account->is_female == 1 )
                                echo "checked = \"checked\"";
                            ?>
                        /><label>Nữ</label>
                    </div>
                </div><!--End form_input_row sexual-->

                <div class="form_input_row">
                    <label class="label_title">Quyền:*</label>
                    <select
                        id      = "sl_authority"
                        name    = "sl_authority"
                        <?php
                        if ($isIEBrowser != true)
                            echo "class = 'form_combobox select_css3 select_two_arrown'";
                        else
                            echo "class = 'form_combobox'";
                        ?>
                        tabindex= "9"
                    >
                        <option value="register">Người Đăng Ký</option>
                        <option value="admin">Người Quản Trị</option>
                    </select>
                </div><!--End form_input_row Authority-->

                <div class="form_input_row">
                    <label class="label_title">E-mail*:</label>
                    
                    <input
                        id            = "txt_email"
                        name        = "txt_email"
                        title        = "Nhập E-mail Của Bạn"
                        type        = "text"
                        value        = "<?php if(isset($o_account)) { echo $o_account->email; }?>"
                        class        = "form_textbox textbox_css3"
                        onblur        = "txt_email_onblur(this,'lbl_email_validation')"
                        tabindex    = "10"
                    />
                    
                    <label
                        id        = "lbl_email_validation" 
                        class    = "lable_validation"
                    >Bạn chưa nhập E-mail.</label>
                </div><!--End form_input_row Email-->
                
                <div class="form_input_row">
                    <label class="label_title">Địa Chỉ:</label>
                    <textarea 
                        id            = "ta_address"
                        name        = "ta_address"
                        rows        = ""
                        cols        = ""
                        class        = "form_textarea textarea_css3"
                        tabindex    = "11"
                    ><?php if(isset($o_account)) { echo $o_account->address; }?></textarea>
                </div><!--End form_input_row Address-->
                
                <div class="form_input_row">
                    <label class="label_title">Số Điện Thoại:</label>
                    
                    <input
                        id            = "txt_phone"
                        name        = "txt_phone"
                        title        = "Nhập Số Điện Thoại Của Bạn"
                        type        = "text"
                        value        = "<?php if(isset($o_account)) { echo $o_account->phone;}?>"
                        class        = "form_textbox textbox_css3"
                        tabindex    = "12"
                    />
                </div><!--End form_input_row Phone-->
                
                <div class="form_input_row">
                    <label class="label_title">Tổ Chức:</label>
                    <select
                        id      = "sl_organization"
                        name    = "sl_organization"
                        <?php
                        if ($isIEBrowser != true)
                            echo "class = 'form_combobox select_css3 select_two_arrown'";
                        else
                            echo "class = 'form_combobox'";
                        ?>
                        tabindex= "13"
                    >
                        <option value=""> </option>
                    </select>
                </div><!--End form_input_row Organization-->
                
                <div class="form_input_row" style="border: none;">
                    <div class="input_items_horizon">
                        <input
                            title        = "gửi yêu cầu"
                            name        = "btn_save"
                            id            = "btn_save"
                            type        = "submit"
                            value        = "Lưu"
                            class        = "input_button button_css3"
                            tabindex    = "14"
                        />
                        <input
                            title        = "gửi yêu cầu"
                            name        = "btn_cancel"
                            id            = "btn_cancel"
                            type        = "button"
                            value        = "Hủy Bỏ"
                            class        = "input_button button_css3"
                            tabindex    = "15"
                        />
                    </div><!--End input_items_horizon-->
                </div><!--End form_input_row button-->

            </fieldset>
        </form>


<script type="text/javascript">
                        
$('#btn_cancel').click(function() {

    $("#dialog_notice").dialog("close");

    var i_key     = <?=$i_key ?>;
    if(i_key != 0)
        $("#tr_"+i_key).css("background-color","#FFFFFF");
});

function validation_onblur( element, lbl_validation) {
    
    if( element.value == ""){
        $('#'+lbl_validation).fadeIn("slow", "linear");
        //$(element).focus();
    } else if ((element.name == "txt_account" || element.name == "txt_password") && element.value.length < 4) {
        $('#'+lbl_validation).fadeIn("slow", "linear");
    } else {
        $('#'+lbl_validation).fadeOut();
    }
}

//check email
//obj: object form to compare
//error_message_id: message id
function txt_email_onblur( element, lbl_validation) {
    var temp = $(element);
    
    if (!echeck(temp.val())) {
        $('#lbl_email_validation').fadeIn("slow", "linear");
        $('#lbl_email_validation').text("Bạn nhập E-mail không đúng định dạng.");
        //temp.focus();
        return;
    }else {
        $('#'+lbl_validation).fadeOut();
    }
}

$('#btn_save').click(function() {

    $( "#dialog_excute" ).dialog( "open" );

    var i_key     = <?=$i_key ?>;
    
    var    i_day    = $("#cb_birth_day").val(),
        i_month    = $("#cb_birth_month").val(),
        i_year    = $("#cb_birth_year").val();
    
    var d_birth = i_year + "-" + i_month + "-" + i_day;

    $.ajax({
        url        : "<?=Yii::app()->createUrl("account/save");?>",
        type    : "POST",
        cache    : false,
        data    : {
            item_key         : i_key,
            txt_name        : $("#txt_name").val(),
            txt_account        : $("#txt_account").val(),
            txt_password    : $("#txt_password").val(),
            d_birth            : d_birth,
            ra_sex            : $("input[name='ra_sex']:checked").val(),
            sl_authority    : $("#sl_authority").val(),
            txt_email        : $("#txt_email").val(),
            ta_address        : $("#ta_address").val(),    
            txt_phone        : $("#txt_phone").val(),
            sl_organization    : $("#sl_organization").val()
        },
        success    : function(string){

            /**
             * Kiểu mặc định trả về là dạng String, bạn dùng hàm parseJSON để phân tích dữ liệu trả về
             * có 2 cách parse JSON là : JSON.parse() và $.parseJSON();
             * 1. var getData = JSON.parse(string);
             * 2. var getData = $.parseJSON(string);
            **/
            var arr_result = $.parseJSON(string);

            if(arr_result.error != null){
                alert(arr_result.error);
                $("#dialog_excute").dialog( "close" );
                return;
            }

            if(i_key != 0) {

                var ct_row = "";
                var today = new Date();
                
                ct_row += "<td>";
                ct_row += "    <input";
                ct_row += "        id        = 'chk_delete'";
                ct_row += "        name    = 'chk_delete'";
                ct_row += "        type    = 'checkbox'";
                ct_row += "        class    = 'check_box'";
                ct_row += "        value    = '"+ i_key +"'";
                ct_row += "    />";
                ct_row += "</td>";
                
                ct_row += "<td>";
                ct_row += "    <span";
                ct_row += "        class    = 'icon_button icon_edit'";
                ct_row += "        onclick    = \"call_edit_page("+ i_key +");\"";
                ct_row += "    > ";
                ct_row += "    </span>";
                ct_row += "</td>";
                                
                ct_row += "<td>Updated</td>";
                ct_row += "<td>" + $("#txt_account").val()    + "</td>";
                ct_row += "<td>" + $("#sl_authority").val()    + "</td>";
                ct_row += "<td>" + $("#txt_name").val()        + "</td>";
                ct_row += "<td>" + $("#txt_email").val()    + "</td>";
                ct_row += "<td>" + $("input[name='ra_sex']:checked").val()+ "</td>";
                ct_row += "<td>" + $("#sl_organization").val()    + "</td>";
                ct_row += "<td>" + today    + "</td>";

                $("#tr_"+i_key).html(ct_row);

                $("#tr_"+i_key).css("background-color","#FFFFFF");

                $("#dialog_excute").dialog( "close" );
                $("#dialog_notice").dialog("close");

            } else if (i_key == 0)
            {
                alert(arr_result.result);
                $("#dialog_excute").dialog( "close" );
            }
        },
        error: function (){
            alert('Trang hiện hành không tồn tại.');
        }
    });
    
});
</script>